#!/bin/bash
[ $# -lt 1 -o $# -gt 2 ] && O="" || {
  O="$1"
  S="$2"
}

soft=("redis" "apollo" "rabbitmq" "nginx" "elasticsearch" "kibana")

Usage() {
  echo " Usage:YUSP {start|stop|status} {redis|apollo|rabbitmq|nginx|elasticsearch|kibana}"
  echo " start: Start service"
  echo " stop:  Stop service"
  echo " status: List state of service"
}

Color_Text() {
  echo -e " \e[0;$2m$1\e[0m"
}

Echo_Red() {
  echo $(Color_Text "$1" "31")
}

Echo_Green() {
  echo $(Color_Text "$1" "32")
}

Start() {
  if [ -n "$1" ]; then
    stat=$(Status $1)
    if [[ $stat =~ "is running" ]]; then
      Echo_Green "$1 is running"
    elif [[ $stat =~ "not install" ]]; then
      Echo_Red "$1 not install"
    else
      if [ $1 == "rabbitmq" -o $1 == "rabbitmq-server" ]; then
        Echo_Green "begin startup $1"
        (while true; do
          echo -e ".\c"
          sleep 1
        done) &
        pid=$!
        /bin/systemctl start rabbitmq-server.service 2>&1 >/dev/null
        if [ $? == 0 ]; then
          kill -9 ${pid}
          echo ""
          Echo_Green "startup $1 ok"
        else
          kill -9 ${pid}
          echo ""
          Echo_Red "startup $1 failed"
        fi
      else
        Echo_Green "begin startup $1"
        (while true; do
          echo -e ".\c"
          sleep 1
        done) &
        pid=$!
        service $1 start 2>&1 >/dev/null
        if [ $? == 0 ]; then
          kill -9 ${pid}
          echo ""
          Echo_Green "startup $1 ok"
        else
          kill -9 ${pid}
          echo ""
          Echo_Red "startup $1 failed"
        fi
      fi
    fi
  else
    for one in ${soft[@]}; do
      stat=$(Status ${one})
      if [[ $stat =~ "is running" ]]; then
        Echo_Green "${one} is running"
      elif [[ $stat =~ "not install" ]]; then
        Echo_Red "${one} not install"
      else
        if [ ${one} == "rabbitmq" -o ${one} == "rabbitmq-server" ]; then
          Echo_Green "begin startup ${one}"
          (while true; do
            echo -e ".\c"
            sleep 1
          done) &
          pid=$!
          /bin/systemctl start rabbitmq-server.service 2>&1 >/dev/null
          if [ $? == 0 ]; then
            kill -9 ${pid}
            echo ""
            Echo_Green "startup $1 ok"
          else
            kill -9 ${pid}
            echo ""
            Echo_Red "startup $1 failed"
          fi
        else
          Echo_Green "begin startup ${one}"
          (while true; do
            echo -e ".\c"
            sleep 1
          done) &
          pid=$!
          service ${one} start 2>&1 >/dev/null
          if [ $? == 0 ]; then
            kill -9 ${pid}
            echo ""
            Echo_Green "startup $1 ok"
          else
            kill -9 ${pid}
            echo ""
            Echo_Red "startup $1 failed"
          fi
        fi
      fi
    done 2>/dev/null
  fi
}

Stop() {
  if [ -n "$1" ]; then
    stat=$(Status $1)
    if [[ $stat =~ "is stopped" ]]; then
      Echo_Red "$1 is stopped"
    elif [[ $stat =~ "not install" ]]; then
      Echo_Red "$1 not install"
    else
      if [ $1 == "rabbitmq" -o $1 == "rabbitmq-server" ]; then
        /bin/systemctl stop rabbitmq-server.service 2>&1 >/dev/null
        if [ $? == 0 ]; then
          Echo_Red "stop $1 ok"
        fi
      else
        service $1 stop 2>&1 >/dev/null
        if [ $? == 0 ]; then
          Echo_Red "stop $1 ok"
        fi
      fi
    fi
  else
    for one in ${soft[@]}; do
      stat=$(Status ${one})
      if [[ $stat =~ "is stopped" ]]; then
        Echo_Red "${one} is stopped"
      elif [[ $stat =~ "not install" ]]; then
        Echo_Red "${one} not install"
      else
        if [ ${one} == "rabbitmq" -o ${one} == "rabbitmq-server" ]; then
          /bin/systemctl stop rabbitmq-server.service 2>&1 >/dev/null
          if [ $? == 0 ]; then
            Echo_Red "stop ${one} ok"
          fi
        else
          service ${one} stop 2>&1 >/dev/null
          if [ $? == 0 ]; then
            Echo_Red "stop ${one} ok"
          fi
        fi
      fi
    done
  fi
}

Status() {
  if [ -n "$1" ]; then
    if [ $1 == "rabbitmq" -o $1 == "rabbitmq-server" ]; then
      if [ -s /usr/sbin/rabbitmq-server ]; then
        stat=$(/bin/systemctl status rabbitmq-server.service)
        [[ $stat =~ "active (running)" ]] && Echo_Green "$1 is running" || Echo_Red "$1 is stopped"
      else
        Echo_Red "$1 not install"
      fi
    else
      if [ -s /etc/init.d/$1 ]; then
        stat=$(service $1 status)
        [[ $stat =~ "is running" || $stat =~ "MySQL running" ]] && Echo_Green "$1 is running" || Echo_Red "$1 is stopped"
      else
        Echo_Red "$1 not install"
      fi
    fi
  else
    for one in ${soft[@]}; do
      if [ ${one} == "rabbitmq" ]; then
        if [ -s /usr/sbin/rabbitmq-server ]; then
          stat=$(/bin/systemctl status rabbitmq-server.service)
          [[ $stat =~ "active (running)" ]] && Echo_Green "${one} is running" || Echo_Red "${one} is stopped"
        else
          Echo_Red "${one} not install"
        fi
      else
        if [ -s /etc/init.d/${one} ]; then
          stat=$(service ${one} status)
          [[ $stat =~ "is running" || $stat =~ "MySQL running" ]] && Echo_Green "${one} is running" || Echo_Red "${one} is stopped"
        else
          Echo_Red "${one} not install"
        fi
      fi
    done
  fi
}

Statusweb() {
  if [ -n "$1" ]; then
    if [ $1 == "rabbitmq" -o $1 == "rabbitmq-server" ]; then
      if [ -s /usr/sbin/rabbitmq-server ]; then
        stat=$(/bin/systemctl status rabbitmq-server.service)
        [[ $stat =~ "active (running)" ]] && Echo_Green "$1 is running" || Echo_Red "$1 is stopped"
      else
        Echo_Red "$1 not install"
      fi
    else
      if [ -s /etc/init.d/$1 ]; then
        stat=$(service $1 status)
        [[ $stat =~ "is running" || $stat =~ "MySQL running" ]] && Echo_Green "$1 is running" || Echo_Red "$1 is stopped"
      else
        Echo_Red "$1 not install"
      fi
    fi
    echo "press any key to continue"
    read a
  else
    for ((i = 0; i < ${#soft[@]}; i++)); do
      if [ ${soft[i]} == "rabbitmq" ]; then
        if [ -s /usr/sbin/rabbitmq-server ]; then
          stat=$(/bin/systemctl status rabbitmq-server.service)
          [[ $stat =~ "active (running)" ]] && Echo_Green "${soft[i]} is running" || Echo_Red "${soft[i]} is stopped"
        else
          Echo_Red "${soft[i]} not install"
        fi
      else
        if [ -s /etc/init.d/${soft[i]} ]; then
          stat=$(service ${soft[i]} status)
          [[ $stat =~ "is running" || $stat =~ "MySQL running" ]] && Echo_Green "${soft[i]} is running" || Echo_Red "${soft[i]} is stopped"
        else
          Echo_Red "${soft[i]} not install"
        fi
      fi
      if [ $(expr ${i} + 1) == ${#soft[@]} ]; then
        echo "press any key to continue"
        read a
      fi
    done
  fi
}

Browse() {
  case "$1" in
  start)
    Start "$2"
    ;;
  stop)
    Stop "$2"
    ;;
  status)
    Status "$2"
    ;;
  statusweb)
    Statusweb "$2"
    ;;
  esac
}

Main() {
  [ -n "$2" ] && {
    echo "YUSP Application Service Manager"
  }

  case "$2" in
  start | stop | status | statusweb)
    Browse "$2" "$3"
    ;;
  *)
    Usage "$1" >&2
    return 1
    ;;
  esac
}

(Main "$0" "${O}" "${S}")
